package com.codeduck.test.job;

import com.codeduck.test.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author: <a href="mailto:codeduck@163.com">codeduck</a>
 * @date: 2022/11/06
 */
@Component
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class DataCleaningDoJob {

    @Resource
    private DataBackupAndCleaning dataBackupAndCleaning;
    @Resource
    private TransDtlMapper transDtlMapper;
    @Resource
    private AlarmInfoMapper alarmInfoMapper;

    public void doJob(String part) {
        long begin = System.currentTimeMillis();
        log.info("<==========数据备份与清理任务开始执行==========>");
        // 数据备份与清理
        backupAndCleaning(part);
        // fileInfo表数据清理
        fileInfoCleaning(part);
        log.info("<==========数据备份与清理任务执行完毕，共耗时[{}]==========>", ( System.currentTimeMillis() - begin) / 1000);
    }

    private void backupAndCleaning(String part) {
        int singleCount = 600;

        // transDtl表数据备份与清理
        int dtlCount = transDtlMapper.selectPartitionRecordCount(part);
        log.info("transDtl - 开始备份与清理交易明细表分区[{}],共[{}]条数据", part, dtlCount);
        int dtlNum = (dtlCount + singleCount + 1 )/ singleCount;
        for (int i = 0; i < dtlNum; i++) {
            int start = i * singleCount;
            int size = singleCount - dtlCount > 0 ? dtlCount : singleCount;
            transDtlDataBackupAndCleaning(part, 0, size);
            dtlCount -= singleCount;
            log.info("transDtl - 备份与清理交易明细表分区[{}]: [{}] 至 [{}] 成功", part, start, start + size);
        }
        log.info("transDtl - 备份与清理交易明细表分区[{}]成功", part);

        // alarmInfo表数据备份与清理
        int infoCount = alarmInfoMapper.selectPartitionRecordCount(part);
        log.info("alarmInfo - 开始备份与清理报警信息表分区[{}],共[{}]条数据", part, infoCount);
        int infoNum = (infoCount + singleCount + 1 )/ singleCount;
        for (int i = 0; i < infoNum; i++) {
            int start = i * singleCount;
            int size = singleCount - infoCount > 0 ? infoCount : singleCount;
            alarmInfoDataBackupAndCleaning(part, 0, size);
            infoCount -= singleCount;
            log.info("alarmInfo - 备份与清理报警信息表分区[{}]: [{}] 至 [{}] 成功", part, start, start + size);
        }
        log.info("alarmInfo - 备份与清理报警信息表分区[{}]成功", part);
    }


    @Resource
    private AlarmInfoHisMapper alarmInfoHisMapper;
    @Resource
    private TransDtlHisMapper transDtlHisMapper;
    @Resource
    private FileInfoMapper fileInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    public void transDtlDataBackupAndCleaning(String part, int start, int size) {
        transDtlHisMapper.insertBeforeDataCleaning(part, start, size);
        transDtlMapper.partitionDataCleaning(part, size);
    }
    @Transactional(rollbackFor = Exception.class)
    public void alarmInfoDataBackupAndCleaning(String part, int start, int size) {
        alarmInfoHisMapper.insertBeforeDataCleaning(part, start, size);
        if (size < 600) {
            throw new RuntimeException("数据备份与清理时发生异常");
        }
        alarmInfoMapper.partitionDataCleaning(part,size);
    }

    public void fileInfoCleaning(String part) {
        fileInfoMapper.partitionDataCleaning(part);
        log.info("文件表分区[{}]数据已清理完毕",part);
    }
}
